﻿﻿using System;
using System.Drawing;
using Microsoft.DirectX;
using Microsoft.DirectX.Direct3D;
using cz.zcu.PreSti.DirectX;
using System.Collections.Generic;

namespace cz.zcu.PreSti.Components
{
    public class Subdiagram : RunnableComponent
    {
        public List<RunnableComponent> entryPoints = new List<RunnableComponent>();
        public List<RunnableComponent> exitPoints = new List<RunnableComponent>();
        public List<RunnableComponent> setters = new List<RunnableComponent>();
        public List<RunnableComponent> getters = new List<RunnableComponent>();
        private string name = "";

        public Subdiagram(IPresentation p, string name)
            : base(p, 1, 1, 7, 7)
        {
            this.name = name;
            p.initSubdiagramComponents(name, ref entryPoints, ref exitPoints, ref setters, ref getters);
            foreach (ExitPoint exitPoint in exitPoints)
                exitPoint.Diagram = this;
            foreach (Setter setter in setters)
                setter.Diagram = this;
            foreach (Getter getter in getters)
                getter.Diagram = this;
        }

        #region parameters



        public String Name
        {
            get
            {
                return name;
            }
        }

        #endregion

        public void updateSetters()
        {
            base.retrieveValuesFromGetters();
        }

        public override void setValueToSetter(int setterN, object value)
        {
            base.setValueToSetter(setterN, value);
            string val = "";
            convertValue(ref val, value);
            foreach (Setter s in setters)
            {
                if (s.SetterIndex == setterN)
                {
                    s.Value = val;
                }
            }
        }

        public override object getValueFromGetter(int getterN)
        {
            foreach (Getter g in getters)
            {
                if (g.GetterIndex == getterN)
                {
                    g.updateSetters();
                    return g.Value;
                }
            }
            return base.getValueFromGetter(getterN);
        }

        public void finish(ExitPoint exitPoint)
        {
            trigOut(0);
        }

        public override void trigIn(int n)
        {
            foreach (RunnableComponent entryPoint in entryPoints)
                entryPoint.trigIn(0);
        }

        public override void update()
        {
        }
    }
}